package com.goods.controller.business;

import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@SuppressWarnings("all")
@RestController
@RequestMapping("/business/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    //获取物资列表
    @GetMapping("/findProductList")
    public ResponseBean<PageVO<ProductVO>> findProductList(int pageNum, int pageSize,
                                                           @RequestParam(value = "name", required = false) String name,
                                                           @RequestParam(value = "categoryId", required = false) Integer categoryId,
                                                           @RequestParam(value = "supplier", required = false) String supplier,
                                                           @RequestParam(value = "status", required = false) Integer status,
                                                           @RequestParam(value = "categorys", required = false) Integer[] categorys) {
        PageVO<ProductVO> productVOList = productService.findProductList(pageNum, pageSize, name, categoryId, supplier, status, categorys);
        return ResponseBean.success(productVOList);
    }

    //添加物资
    @PostMapping("/add")
    public ResponseBean add(@RequestBody Map addRuleForm) {
        productService.add(addRuleForm);
        return ResponseBean.success();
    }

    //修改物资时回显物资信息
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id) {
        Product product = productService.selectProductById(id);
        return ResponseBean.success(product);
    }

    //修改物资信息
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id, @RequestBody Map editRuleForm) {
        productService.update(id, editRuleForm);
        return ResponseBean.success();
    }

    //移除物资信息到回收站
    @PutMapping("/remove/{id}")
    public ResponseBean remove(@PathVariable Long id) {
        productService.remove(id);
        return ResponseBean.success();
    }

    //从回收站恢复物资信息
    @PutMapping("/back/{id}")
    public ResponseBean back(@PathVariable Long id) {
        productService.back(id);
        return ResponseBean.success();
    }

    //审核通过物资
    @PutMapping("/publish/{id}")
    public ResponseBean publish(@PathVariable Long id) {
        productService.publish(id);
        return ResponseBean.success();
    }

    //删除物资信息
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id) {
        productService.delete(id);
        return ResponseBean.success();
    }

    //获取所有物资信息
    @GetMapping("/findProducts")
    public ResponseBean<PageVO<ProductVO>> findProducts(int pageNum, int pageSize, Integer status) {
        PageVO<ProductVO> productVOList = productService.findProducts(pageNum, pageSize, status);
        return ResponseBean.success(productVOList);
    }

}
